class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int left = 0;
        int right = 0;
        int count = INT_MIN;
        int n = 0;

        while (right < nums.size())
        {
            if (nums[right] == 0)
            {
                n++;
            }
            while (n > k)
            {
                if (nums[left] == 0)
                {
                    n--;
                }
                left++;
            }
            count = max(count, right - left + 1);
            right++;
        }
        return count == INT_MIN ? 0 : count;
    }
};